﻿namespace PrismEx.SampleApp.Modules.SplashScreen
{
    using System;
    using System.ComponentModel.Composition;
    using System.Threading;
    using System.Windows;
    using System.Windows.Interop;

    using Microsoft.Practices.Prism.Events;

    using PrismEx.Events;

    [Export]
    public class SplashScreenService
    {
        private readonly IEventAggregator eventAggregator;

        [ImportingConstructor]
        public SplashScreenService(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }

        public void Start()
        {
            var parent = Application.Current.MainWindow;

            var parentDetails = new ParentDetails
                {
                    Handle = new WindowInteropHelper(parent).Handle,
                    Width = (int)parent.Width,
                    Height = (int)parent.Height
                };

            var t = new Thread(() =>
            {
                var splashScreen = new Splash(parentDetails);
                eventAggregator.Subscribe<CloseSplashScreenEvent>(e => splashScreen.Dispatcher.Invoke((Action)(splashScreen.Close)));
                splashScreen.ShowDialog();
            });

            t.Name = "Splash Screen Thread";
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
    }


}
